package com.lzwork.gpt.utility.thread;

import com.lzwork.utility.local.ConsoleJob;

/**
 * @author LiZheng
 * @date 2024年9月2日 11:00:11
 */

public class InteruptTest extends ConsoleJob
{
	Thread localThread;
	boolean threadOn = true;

	@Override
	protected CommandState beforeLoop()
	{
		localThread = new Thread("LocalTest-" + System.currentTimeMillis())
		{
			public void run()
			{
				try
				{
					while (!Thread.currentThread().isInterrupted())
					{
					}
					log("Local thread get interrupted.");

					localThread = null;

				} catch (Exception e)
				{
					e.printStackTrace();
				}

			}
		};
		localThread.start();
		log("Start local thread: " + localThread.getId());
		return CommandState.Done;
	}

	@Override
	protected CommandState afterLoop()
	{
		if (localThread != null)
		{
			threadOn = false;
		}

		return CommandState.Skip;
	}

	@Override
	protected CommandState dealInput(String input)
	{
		CommandState state = CommandState.Done;

		switch (input)
		{
		case "info":
			showInfo();
			break;
		case "stop":
			if (localThread == null)
				log("Local thread is null.");
			else
			{
				localThread.interrupt();
				log("Call interrupt.");
			}
			break;
		case "resume":
			if (localThread == null)
				log("Local thread is null.");
			else
			{
				//
			}
			break;			
		default:
			state = CommandState.Skip;
			break;
		}

		return state;
	}

	void showInfo()
	{
		if (localThread == null)
		{
			log("Thread is NULL");
			return;
		}

		if (localThread.isAlive())
			log("Thread is alive");

		if (localThread.isInterrupted())
			log("Thread is interrupted");
	}

	public static void main(String[] args)
	{
		InteruptTest it = new InteruptTest();
		it.start();
	}
}
